package com.zhou.android.main;

import android.Manifest;
import android.content.pm.PackageManager;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.util.Size;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zhou.android.R;
import com.zhou.android.camera2.CameraUtil;
import com.zhou.android.common.BaseActivity;
import com.zhou.android.common.Utils;
import com.zhou.android.opengl.GLFrameRenderer;

import java.nio.ByteBuffer;

import io.reactivex.disposables.Disposable;

/**
 * CameraUtil Test
 * Created by Administrator on 2018/10/31.
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class CameraUtilTestActivity extends BaseActivity {

    private CameraUtil cameraUtil;

    private ByteBuffer doubleBuffer, tmpBuffer;
    private byte[] tmpCopy;

    private SurfaceView surface;
    private GLSurfaceView glSurface;
    private GLFrameRenderer renderer;
    private TextureView textureView;
    private Button btnControl, btnSwitch;

    private Disposable disposable;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_camera_util);
    }

    @Override
    protected void init() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            String msg = "系统版本需高于安卓 5.0，当前版本（" + Build.VERSION.RELEASE + "），暂无法使用";
            Log.e("CameraTest", msg);
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
            return;
        }

        disposable = Utils.checkPermission(this, Manifest.permission.CAMERA, "相机");

        textureView = findViewById(R.id.textureView);
        surface = findViewById(R.id.surface);
//        cameraUtil = new CameraUtil(this, textureView, surface);

        glSurface = findViewById(R.id.glSurface);
        glSurface.setEGLContextClientVersion(2);
        renderer = new GLFrameRenderer(glSurface);
        glSurface.setRenderer(renderer);
        glSurface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

        btnControl = findViewById(R.id.btnControl);
        btnControl.setSelected(false);
        btnSwitch = findViewById(R.id.btnSwitch);

    }

    @Override
    protected void addListener() {
        btnSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cameraUtil != null) {
                    cameraUtil.switchCamera();
                }
            }
        });
        btnControl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean cur = btnControl.isSelected();
                if (cameraUtil != null) {
                    if (!cur) {
                        cameraUtil.startPreview(CameraUtilTestActivity.this);
                        btnSwitch.setEnabled(true);
                    } else {
                        cameraUtil.stopPreview();
                        btnSwitch.setEnabled(false);
                    }
                    btnControl.setText(!cur ? "关闭" : "启动");
                    btnControl.setSelected(!cur);
                }
            }
        });

        if (cameraUtil != null) {

            final Size size = cameraUtil.getSize();
            final int capacity = size.getWidth() * size.getHeight();
            doubleBuffer = ByteBuffer.allocate(size.getWidth() * size.getHeight() * 3);
            tmpCopy = new byte[(int) (size.getWidth() * size.getHeight() * 1.5)];

//            cameraUtil.setPreviewFrameCallback(new CameraUtil.OnPreviewFrameCallback() {
//                @Override
//                public void onCameraFront(byte[][] bytes, int orientation) {
//                    if (renderer != null && bytes.length == 3) {
//                        renderer.update(bytes[0], bytes[1], bytes[2]);
//                    }
//                }
//
//                @Override
//                public void onCameraBack(byte[][] bytes, int orientation) {
//                    if (renderer != null && bytes.length == 3) {
//                        renderer.update(bytes[0], bytes[1], bytes[2]);
//                    }
//                }
//
//                @Override
//                public void onCameraFront(byte[] bytes, int orientation) {
//
////                    if (semaphore.tryAcquire()) {
////                        try {
////                            doubleBuffer.put(bytes, 0, capacity);
////                            doubleBuffer.put(tmpCopy, 0, capacity);
////                            doubleBuffer.put(bytes, capacity, capacity / 4);
////                            doubleBuffer.put(tmpCopy, capacity, capacity / 4);
////                            doubleBuffer.put(bytes, capacity * 5 / 4, capacity / 4);
////                            doubleBuffer.put(tmpCopy, capacity * 5 / 4, capacity / 4);
////
////                            doubleBuffer.flip();
////                            byte[] data = doubleBuffer.array();
////                            doubleBuffer.flip();
////                            doubleBuffer.clear();
////                        } finally {
////                            semaphore.release();
////                        }
////                    }
//
//                    //发送
//                }
//
//                @Override
//                public void onCameraBack(byte[] bytes, int orientation) {
////                    try {
////                        if (semaphore.tryAcquire(100, TimeUnit.MILLISECONDS)) {
////                            tmpCopy = bytes;
////                            semaphore.release();
////                        }
////                    } catch (InterruptedException e) {
////                        e.printStackTrace();
////                    }
//                }
//            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        int r = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        if (PackageManager.PERMISSION_GRANTED == r && cameraUtil == null) {
            cameraUtil = new CameraUtil(this, textureView, surface);
        }

        if (cameraUtil != null && !renderer.isInit()) {

            glSurface.post(new Runnable() {
                @Override
                public void run() {
                    int w = glSurface.getWidth();
                    int h = glSurface.getHeight();
                    Log.d("camera_util", "width = " + w + " , height = " + h);

                    Size size = cameraUtil.getSize();
                    renderer.update(w, h, size.getWidth(), size.getHeight());
                }
            });
        }

//        if (cameraUtil != null) {
//            cameraUtil.startPreview(this);
//        }
    }

    @Override
    protected void onPause() {
        if (cameraUtil != null) {
            cameraUtil.release();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }
}
